<script setup>
import {ref} from 'vue'
import {ElMessage} from 'element-plus'
import useUserInfoStore from "@/stores/userInfo.js"
import {updatePwdService} from "@/api/user.js";
// 假设有封装好的API请求函数

const userInfoStore = useUserInfoStore()

const formRef = ref()  // 表单引用
const updatePwdData = ref({
    old_pwd: '',
    new_pwd: '',
    re_pwd: ''
})

// 验证确认密码是否一致
const validateRePwd = (rule, value, callback) => {
    if (value === '') {
        callback(new Error('请再次确认密码'))
    } else if (value !== updatePwdData.value.new_pwd) {
        callback(new Error('两次输入的密码不一致'))
    } else {
        callback()
    }
}

const rules = {
    old_pwd: [
        {required: true, message: '请输入旧密码', trigger: 'blur'},
        {min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur'}
    ],
    new_pwd: [
        {required: true, message: '请输入新密码', trigger: 'blur'},
        {min: 5, max: 16, message: '长度为5~16位非空字符', trigger: 'blur'}
    ],
    re_pwd: [
        {required: true, message: '请再次输入新密码', trigger: 'blur'},
        {validator: validateRePwd, trigger: 'blur'}
    ]
}

// 提交修改
const updatePwd = async () => {
    let result = await updatePwdService(updatePwdData.value);
    ElMessage.success(result.msg ? result.msg : '修改成功')
}
</script>

<template>
    <el-card class="page-container">
        <template #header>
            <div class="header">
                <span>修改密码</span>
            </div>
        </template>
        <el-row>
            <el-col :span="12">
                <el-form
                        ref="formRef"
                        :model="userInfo"
                        :rules="rules"
                        label-width="100px"
                        size="large"
                >
                    <el-form-item label="登录名称">
                        <el-input v-model="userInfoStore.info.username" disabled></el-input>
                    </el-form-item>
                    <el-form-item label="旧密码" prop="old_pwd">
                        <el-input
                                v-model="updatePwdData.old_pwd"
                                type="password"
                                show-password
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="新密码" prop="new_pwd">
                        <el-input
                                v-model="updatePwdData.new_pwd"
                                type="password"
                                show-password
                        ></el-input>
                    </el-form-item>
                    <el-form-item label="确认密码" prop="re_pwd">
                        <el-input
                                v-model="updatePwdData.re_pwd"
                                type="password"
                                show-password
                        ></el-input>
                    </el-form-item>
                    <el-form-item>
                        <el-button type="primary" @click="updatePwd">提交修改</el-button>
                    </el-form-item>
                </el-form>
            </el-col>
        </el-row>
    </el-card>
</template>